package com.yhd.mapper;

import com.yhd.domain.Employee;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 基本的CRUD
 * @author 二十
 * @since 2021/11/30 10:23 下午
 */
public interface EmployeeMapper {

    @Results(id = "emp",
            value = {
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "emp_name", property = "EmpName"),
                    @Result(column = "age", property = "age"),
                    @Result(column = "dept_name", property = "deptName"),
                    @Result(column = "salary", property = "salary"),
            })
    @Select("select id ,emp_name,age,dept_name,salary from employee;")
    List<Employee> findAll();

    @ResultMap("emp")
    @Select("select id ,emp_name,age,dept_name,salary from employee where emp_name like concat('%',#{empName},'%');")
    List<Employee> nameLike(String empName);

    @ResultMap("emp")
    @Select("select id ,emp_name,age,dept_name,salary from employee where id = #{id};")
    Employee findById(Integer id);

    @Select("select count(*) from employee;")
    Integer count();
    @Options(useGeneratedKeys = true,keyProperty ="id",keyColumn = "id")
    //@SelectKey(resultType = Integer.class, statement = "select last_insert_id()", before = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into employee(emp_name,age,dept_name,salary)values(#{EmpName},#{age},#{deptName},#{salary})")
    int save(Employee employee);

    @Update("update employee set emp_name=#{EmpName},age=#{age},dept_name=#{deptName},salary=#{salary} where id=#{id};")
    void update(Employee employee);

    @Delete("delete from employee where id = #{id};")
    void delete(Integer id);

    @Delete("<script>"+"delete from employee where id in\n" +
            "        <foreach collection=\"array\" item=\"id\" separator=\",\" open=\"(\" close=\")\">\n" +
            "            #{id}\n" +
            "        </foreach>"+"</script>")
    void dels(Integer[] ids);
}
